import * as _ from './utils/underscore';

// 乱序

var arr = [1,2,3,4,5,6];
// arr.sort(() => Math.random() - 0.5);

// console.log(arr);

var times = [0,0,0,0,0];
for (var i = 0; i < 100000; i++) {
  let arr = [1, 2, 3, 4, 5];
  arr.sort(() => Math.random() - 0.5);
  times[arr[4] - 1]++;
}

console.log(times); // 频率并不接近

// sort的实现方式不用浏览器不一样
// v8引擎 当目标数组长度小于10时，使用插入排序；反之，使用快速排序和插入排序的混合排序。

// 插入排序的源码
function InsertionSort (arr, from, to) {
  for (var i = from + 1; i < to; i++) {
    var element = arr[i];
    for (var j = i - 1; j >= from; j--) {
      var tmp = arr[j];
      if (tmp > element) {
        arr[j + 1] = tmp;
      } else {
        break;
      }
    }
    arr[j + 1] = element;
  }
}

// var arr = [5,23,7,4,9,3,10];
// InsertionSort(arr, 4, arr.length);
// console.log(arr);

// Fisher–Yates

function shuffle (arr) {
  for (let i = arr.length; i; i--) {
    let j = Math.floor(Math.random() * i);
    [arr[i - 1], arr[j]] = [arr[j], arr[i - 1]];
  }
  return arr;
}

// console.log(shuffle(arr));

var times = 100000;
var res = {};

for (var i = 0; i < times; i++) {
  let arr = shuffle([1,2,3]);
  let key = JSON.stringify(arr);
  res[key] ? res[key]++ : res[key] = 1;
}

for (var i in res) {
  res[i] = res[i] / times * 100 + '%'
}

console.log(res);
